<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%!
    // 院内版添加token
    public Message addToken(HospitalDeviceToken deviceToken) {
        Message msg = new Message();
        try {
            Integer hospital = deviceToken.getHospital();
            String token = Tools.getDesc(deviceToken.getToken(), "");
            String version = Tools.getDesc(deviceToken.getVersion(), "");
            Integer uid = deviceToken.getUid();
            if (hospital == null || uid == null || "".equals(token) || "".equals(version)) {
                return new Message(30002, "添加设备号失败");
            }
            List<HospitalDeviceToken> dList = QBuilder.db("basedb").select().from("HospitalDeviceToken").where(new Where("hospital=", hospital).and("token=", token).and("uid=", uid).and("status=", STATUS_ENABLE)).page(1,1).query().getResultObj();
            if(dList.size() == 0) {
                DBTools.db("basedb").addItem(deviceToken);
            } else {
                HospitalDeviceToken old = dList.get(0);
                deviceToken.setcTime(old.getcTime());
                deviceToken.setId(old.getId());
                DBTools.db("basedb").modItem(deviceToken, deviceToken.getId());
            }
        } catch (Exception e) {
            return new Message(30002, "添加设备号失败");
        }
        return msg;
    }
%>
<%
    // iOS 1  Android 2
    Integer client = input.getInt("client", 1);
    Integer uid = input.getInt("uid", -1);
    String version = input.getDesc("version");
    String token = input.getDesc("device_token");
    HospitalDeviceToken deviceToken = new HospitalDeviceToken();
    deviceToken.setHospital(hospital);
    deviceToken.setClient(client);
    deviceToken.setToken(token);
    deviceToken.setUid(uid);
    deviceToken.setVersion(version);
    Tools.encryptPrint(out, addToken(deviceToken));
%>
